package com.qidian.mespro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mespro.mapper.ProcessStepMapper;
import com.qidian.mespro.service.ProcessStepService;
import com.qidianit.mes.pojo.ProcessStep;
import com.qidianit.mes.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ author: 周晓轩
 * @ company 起点编程
 * @ time: 2025/9/18 - 10:17
 * @ Description:
 */
@Service
public class ProcessStepServiceImpl implements ProcessStepService {

 @Autowired
 private ProcessStepMapper processStepMapper;

 @Override
 public Result<PageInfo<ProcessStep>> selectProcessStepList(Long processId, int pageNum, int pageSize) {
  PageHelper.startPage(pageNum,pageSize);
  List<ProcessStep> processSteps = processStepMapper.selectProcessStepList(processId);
  return Result.success(new PageInfo<>(processSteps));
 }

 @Override
 public Result<ProcessStep> selectByProcessStepId(Long id) {
  return Result.success(processStepMapper.selectByProcessStepId(id));
 }

 @Override
 public Result<ProcessStep> addProcessStep(ProcessStep processStep) {
  if (processStep.getProcessId() == null) {
   return Result.error("工序ID不能为空");
  }
  if (processStep.getStepOrder() == null) {
   return Result.error("步骤顺序不能为空");
  }

  int count = processStepMapper.countByProcessIdAndStepOrder(processStep.getProcessId(), processStep.getStepOrder());
  if (count > 0) {
   return Result.error("当前工序下已存在相同的步骤顺序，请修改后重试");
  }
   return processStepMapper.addProcessStep(processStep)== 1?Result.success(processStep):Result.error("保存失败");
 }

 @Override
 public Result updateProcessStep(ProcessStep processStep) {
  return processStepMapper.updateProcessStep(processStep) == 1 ? Result.success():Result.error("编辑失败");
 }

 @Override
 public Result deleteProcessStepById(Long id) {
  return processStepMapper.deleteProcessStepById(id) == 1?Result.success():Result.error("删除失败");
 }
}
